using System;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using WebApi.Api.Entity;
using System.Linq;
using Newtonsoft.Json;
using WebApi.Api.ParamModel;
using WebApi.Api.Repository;

namespace MyWebApi.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private IRepository<Users> _usersRespository;

        public UsersController(IRepository<Users> usersRespository)
        {
            _usersRespository = usersRespository;
        }

        [HttpGet]
        public dynamic Get()
        {
            var users = _usersRespository.Table.ToList();
            return new
            {
                Code = 1020,
                Data = users,
                Msg = "获取用户列表成功"
            };
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var user = _usersRespository.GetById(id);
            return user;
        }

        [HttpPost]
        public string Post(NewUser model)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();

            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

            var user = new Users
            {
                Username = model.Username,
                Password = model.Password
            };

            _usersRespository.Insert(user);

            var res = new
            {
                Code = 1000,
                Data = user,
                Msg = "创建用户成功"
            };
            return JsonConvert.SerializeObject(res, settings);
        }

        [HttpPut("{id}")]
        public dynamic Put(int id, NewUser model)
        {
            var user = _usersRespository.GetById(id);

            if (user != null)
            {
                user.Username = model.Username;
                user.Password = model.Password;
                user.UpdatedTime = DateTime.Now;
                _usersRespository.Update(user);
                return new
                {
                    Code = 1000,
                    Data = user,
                    Msg = string.Format("你修改的用户的id为：{0}，已经修改成功，请注意查收", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                };
            }
        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var user = _usersRespository.GetById(id);
            if (user != null)
            {
                _usersRespository.Delete(id);
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除指定id为{0}的用户成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "指定id的用户不存在，请确认重试"
                };
            }
        }

        private IEnumerable<Users> GetUsers()
        {
            var users = new List<Users>{
                new Users{
                    Id=1,
                    Username="你好",
                    Password="确实好啊"
                },
                new Users{
                    Id=2,
                    Username="真的吗",
                    Password="不啊"
                },
                new Users{
                    Id=3,
                    Username="可以吗",
                    Password="我觉得不行"
                },
            };
            return users;
        }
    }
}
